package com.model.common.ui.activity.initial

import android.content.Intent
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.viewpager.widget.ViewPager
import com.model.common.R
import com.model.common.base.activity.BaseActivity
import com.model.common.databinding.ActivityGuideBinding
import com.model.common.ui.adapter.GuidePageAdapter

/**
 * Create by Carson on 2021/12/22.
 * GuideActivity --- 引导页
 */
class GuideActivity : BaseActivity<ActivityGuideBinding>() {

    //图片资源的数组
    private var imageIdArray = intArrayOf()

    //图片资源的集合
    private val viewList = ArrayList<View>()

    override fun getViewBinding(): ActivityGuideBinding =
        ActivityGuideBinding.inflate(layoutInflater)

    override fun initViewsAndEvents() {
        //加载ViewPager
        initViewPager()
    }

    /**
     * 加载图片的viewPager
     */
    private fun initViewPager() {
        //实例化图片资源
        imageIdArray =
            intArrayOf(R.drawable.welcome_one, R.drawable.welcome_two, R.drawable.welcome_three)
        //获取一个layout参数，设置为全屏
        val params = LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT
        )

        //循环创建View并加入到集合当中
        for (index in imageIdArray) {
            //new ImageView并设置全屏和图片资源
            val imageView = ImageView(this)
            imageView.scaleType = ImageView.ScaleType.FIT_XY
            imageView.layoutParams = params
            imageView.setBackgroundResource(index)

            //将imageView加入到集合中
            viewList.add(imageView)
        }

        binding.viewPager.adapter = GuidePageAdapter(viewList)
    }

    override fun addListener() {
        super.addListener()
        //立即体验
        binding.guideStart.setOnClickListener {
            val preferences = getSharedPreferences("save_first_run", 0)
            val editor = preferences.edit()
            editor.putBoolean("isFirstRun", false)
            editor.apply()
            //进入主界面
            val className = Class.forName("com.carson.undergo.ui.activity.MainActivity")
            val intent = Intent(this, className)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            startActivity(intent)
            finish()
        }

        //viewPager滑动监听
        binding.viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int,
            ) {
                //判断是否是最后一页,若是则显示按钮
                if (position == imageIdArray.size - 1) {
                    binding.guideStart.visibility = View.VISIBLE
                } else {
                    binding.guideStart.visibility = View.GONE
                }
            }

            override fun onPageSelected(position: Int) {

            }

            override fun onPageScrollStateChanged(state: Int) {

            }
        })
    }
}